package com.um.jdy.platform.device.manger;

import com.um.jdy.common.device.code.DeviceCode;
import com.um.jdy.common.device.entity.dto.PileDTO;
import com.um.jdy.common.device.entity.po.Pile;
import com.um.jdy.common.device.entity.vo.PileVO;
import com.um.jdy.common.device.enums.DeviceEnum;
import com.um.jdy.common.device.service.PileService;
import com.um.jdy.platform.device.entity.dto.SetEBoxParamsDTO;
import com.um.jdy.platform.device.entity.dto.SetEVParamsDTO;
import com.um.jdy.platform.device.entity.dto.SetPBParamsDTO;
import com.um.jdy.platform.device.entity.dto.SetEBParamsDTO;
import com.um.jdy.rpc.entity.EBDeviceParams;
import com.um.jdy.rpc.entity.EVDeviceParams;
import com.um.jdy.rpc.entity.xh.EBXhDeviceParams;
import com.um.jdy.rpc.entity.ykc.EVYkcDeviceParams;
import com.um.jdy.rpc.factory.ServiceFactory;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.manager.BaseCMSManager;
import com.um.springboot.starter.orm.manager.BaseManager;
import com.um.springboot.starter.utils.IdUtils;
import com.um.springboot.starter.utils.JSONUtil;
import com.um.springcloud.dubbo.cluster.DeviceKey;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 管理类：充电桩参数
 */
@Service
@Slf4j
public class PileParamsManager extends BaseManager<PileService> {

    @Autowired
    ServiceFactory serviceFactory;


    /**
     * 设置单车桩参数
     *
     * @param setEBParamsDTO
     * @return
     */
    public Response setEBParams(SetEBParamsDTO setEBParamsDTO) {
        if (setEBParamsDTO.getCode().equals("")) {
            return Response.failed("设备号不能为空！");
        }

        Pile pile = service.getEntity(new String[]{"code"}, new Object[]{setEBParamsDTO.getCode()});
        if (pile == null) {
            return Response.failed(DeviceCode.NoDevice);
        }

        // 设备离线
        if (!DeviceEnum.DeviceStatus.Online.name().equals(pile.getDeviceStatus())) {
            return Response.failed(DeviceCode.DeviceOffline);
        }

        if (setEBParamsDTO.getPower() > 100) {
            return Response.failed("充满自停功率不能大于100W！");
        }

        if (setEBParamsDTO.getMoney() > 250) {
            return Response.failed("单次投币时长不能大于250分钟！");
        }

        if (setEBParamsDTO.getTemp() > 100) {
            return Response.failed("高温报警温度不能大于100！");
        }

        if (setEBParamsDTO.getOffTime1() > 100) {
            return Response.failed("充满自停关闭时间不能大于65535秒！");
        }

        if (setEBParamsDTO.getOffTime2() > 100) {
            return Response.failed("插头拨出关闭时间不能大于250秒！");
        }

        setEBParamsDTO.setPower(Math.max(1, setEBParamsDTO.getPower()));
        setEBParamsDTO.setMoney(Math.max(0, setEBParamsDTO.getMoney()));
        setEBParamsDTO.setTemp(Math.max(1, setEBParamsDTO.getTemp()));
        setEBParamsDTO.setOffTime1(Math.max(1, setEBParamsDTO.getOffTime1()));
        setEBParamsDTO.setOffTime2(Math.max(1, setEBParamsDTO.getOffTime2()));



        try {

            pile.setParameters(JSONUtil.toJSONString(setEBParamsDTO));
            service.save(pile);

            EBDeviceParams ebDeviceParams = new EBXhDeviceParams();
            BeanUtils.copyProperties(setEBParamsDTO, ebDeviceParams);

            serviceFactory.getRpcEBDeviceService(pile.getProtocol()).reqSetParams(IdUtils.getPrimaryKey(), new DeviceKey(pile.getImei()), ebDeviceParams);

            return Response.success();

        } catch (Exception ex) {
            return Response.failed("单车桩设置失败！");
        }
    }

    /**
     * 设置充电柜参数
     *
     * @param setEBoxParamsDTO
     * @return
     */
    public Response setEBoxParams(SetEBoxParamsDTO setEBoxParamsDTO) {
        if (setEBoxParamsDTO.getCode().equals("")) {
            return Response.failed("设备号不能为空！");
        }

        Pile pile = service.getEntity(new String[]{"code"}, new Object[]{setEBoxParamsDTO.getCode()});
        if (pile == null) {
            return Response.failed(DeviceCode.NoDevice);
        }

        // 设备离线
        if (!DeviceEnum.DeviceStatus.Online.name().equals(pile.getDeviceStatus())) {
            return Response.failed(DeviceCode.DeviceOffline);
        }

        if (setEBoxParamsDTO.getPower() > 100) {
            return Response.failed("充满自停功率不能大于100W！");
        }

        if (setEBoxParamsDTO.getMoney() > 250) {
            return Response.failed("单次投币时长不能大于250分钟！");
        }

        if (setEBoxParamsDTO.getTemp() > 100) {
            return Response.failed("高温报警温度不能大于100！");
        }

        if (setEBoxParamsDTO.getOffTime1() > 100) {
            return Response.failed("充满自停关闭时间不能大于65535秒！");
        }

        if (setEBoxParamsDTO.getOffTime2() > 100) {
            return Response.failed("插头拨出关闭时间不能大于250秒！");
        }

        setEBoxParamsDTO.setPower(Math.max(1, setEBoxParamsDTO.getPower()));
        setEBoxParamsDTO.setMoney(Math.max(0, setEBoxParamsDTO.getMoney()));
        setEBoxParamsDTO.setTemp(Math.max(1, setEBoxParamsDTO.getTemp()));
        setEBoxParamsDTO.setOffTime1(Math.max(1, setEBoxParamsDTO.getOffTime1()));
        setEBoxParamsDTO.setOffTime2(Math.max(1, setEBoxParamsDTO.getOffTime2()));


        try {

            pile.setParameters(JSONUtil.toJSONString(setEBoxParamsDTO));
            service.save(pile);

            EBDeviceParams ebDeviceParams = new EBXhDeviceParams();
            BeanUtils.copyProperties(setEBoxParamsDTO, ebDeviceParams);

            serviceFactory.getRpcEBoxDeviceService(pile.getProtocol()).reqSetParams(IdUtils.getPrimaryKey(), new DeviceKey(pile.getImei()), ebDeviceParams);

            return Response.success();

        } catch (Exception ex) {
            return Response.failed("充电柜设置失败！");
        }
    }

    /**
     * 设置汽车桩参数
     *
     * @param setEVParamsDTO
     * @return
     */
    public Response setEVParams(SetEVParamsDTO setEVParamsDTO) {
        if (setEVParamsDTO.getCode().equals("")) {
            return Response.failed("设备编号不能为空！");
        }

        Pile pile = service.getEntity(new String[]{"code"}, new Object[]{setEVParamsDTO.getCode()});
        if (pile == null) {
            return Response.failed(DeviceCode.NoDevice);
        }

        // 设备离线
        if (!DeviceEnum.DeviceStatus.Online.name().equals(pile.getDeviceStatus())) {
            return Response.failed(DeviceCode.DeviceOffline);
        }

        if (setEVParamsDTO.getMaxPower() > 100) {
            return Response.failed("充电桩最大允许输出功率不能大于100%！");
        }

        if (setEVParamsDTO.getMaxPower() < 1) {
            return Response.failed("充电桩最大允许输出功率不能大于1%！");
        }

        if(setEVParamsDTO.getUseStatus() != 0 && setEVParamsDTO.getUseStatus() != 1){
            setEVParamsDTO.setUseStatus(0);
        }

        try {

            pile.setParameters(JSONUtil.toJSONString(setEVParamsDTO));
            service.save(pile);

            EVYkcDeviceParams evDeviceParams = new EVYkcDeviceParams();
            evDeviceParams.setUseStatus(setEVParamsDTO.getUseStatus());
            evDeviceParams.setMaxPower(setEVParamsDTO.getMaxPower());

            serviceFactory.getRpcEVDeviceService(pile.getProtocol()).setParameters(new DeviceKey(pile.getImei()), evDeviceParams);

            return Response.success();

        } catch (Exception ex) {
            log.error("充电桩参数设置失败：{}",ex.getMessage());
            return Response.failed("充电桩参数设置失败！");
        }
    }

    /**
     * 设置充电宝参数
     *
     * @param setPBParamsDTO
     * @return
     */
    public Response setPBParams(SetPBParamsDTO setPBParamsDTO) {

        Pile pile = service.getEntity(new String[]{"code"}, new Object[]{setPBParamsDTO.getCode()});
        if (pile == null) {
            return Response.failed(DeviceCode.NoDevice);
        }

        // 设备离线
        if (!DeviceEnum.DeviceStatus.Online.name().equals(pile.getDeviceStatus())) {
            return Response.failed(DeviceCode.DeviceOffline);
        }

        setPBParamsDTO.setHeart(Math.max(1, setPBParamsDTO.getHeart()));
        setPBParamsDTO.setPortHeart(Math.max(1, setPBParamsDTO.getPortHeart()));

        try {

            pile.setHeart(Math.max(1, setPBParamsDTO.getHeart()));
            pile.setPortHeart(Math.max(1, setPBParamsDTO.getPortHeart()));
            pile.setParameters(JSONUtil.toJSONString(setPBParamsDTO));
            service.save(pile);

            //发送重启指令
            serviceFactory.getRpcPBDeviceService(pile.getProtocol()).reqRestart(IdUtils.getPrimaryKey(), new DeviceKey(pile.getImei()));

            return Response.success();

        } catch (Exception ex) {
            return Response.failed("充电宝设置失败！");
        }
    }

}
